import os
import re
import requests
from animation.config import Base_Host
class Animation():
    def get_img(self, page,save_path):
        header={"User-Agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/122.0.0.0 Safari/537.36"}
        url = Base_Host.format(page)
        resp = requests.post(url=url, headers=header)
        for i in range(30):
            img_url=resp.json().get('data')[i].get('hoverURL')
            img_name=resp.json().get('data')[i].get('fromPageTitle')
            os.makedirs(save_path,exist_ok=True)
            filename=img_name+'.jpg'
            update_filename=re.sub(r'[\\/*?:"<>|]','_',filename)
            save_path1=os.path.join(save_path,update_filename)
            response=requests.get(url=img_url)
            if response.status_code==200:
                with open(save_path1,'wb') as file:
                    file.write(response.content)
                print('图片已保存到{}'.format(save_path1))
            else:
                print('请求图片失败，状态码为：{}'.format(response.status_code))
if __name__ == '__main__':
    img=Animation()
    save_path=r'C:\Users\Administrator\Desktop\图片'
    img_list=[30]
    for i in img_list:
        resp=img.get_img(i,save_path)
